package org.dlsu.pdfgen.gui;


import com.sun.pdfview.PDFFile;
import com.sun.pdfview.PDFPage;
import java.awt.Image;
import java.awt.Rectangle;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import javax.swing.ImageIcon;

/**
 *
 * @author Vitug
 */
public class previewWindow extends javax.swing.JFrame {

    PDFFile pdfFile;
    int pageNum = 1;
    int totalPages = 0;
    Image image;
    Rectangle rect;

    /** 
     * Constructor for previewWindow class
     * 
     * @param String source
     * 			The location of the source pdf file.
     */
    public previewWindow(String source) throws FileNotFoundException, IOException {
        initComponents();
        this.setTitle("Syllabus Formatter Preview");
        this.setVisible(true);

        loadPDF(source);

        totalPages = pdfFile.getNumPages();
        totalPagesLabel.setText("/ " + totalPages);

        pageNumField.setText("" + pageNum);
        PDFPage page = pdfFile.getPage(pageNum);

        setPagetoImage();

    }

    /** 
     * Loads the pdf file into the application.
     * 
     * @param String source
     * 			The location of the source pdf file.
     */
    private void loadPDF(String source) throws FileNotFoundException, IOException {
        RandomAccessFile raf = new RandomAccessFile(new File(source), "r");
        FileChannel fc = raf.getChannel();
        ByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        pdfFile = new PDFFile(buf);
    }

    /** 
     * Sets the current page to be displayed.
     * 
     * 
     */
    private void setPagetoImage() {
        PDFPage page = pdfFile.getPage(pageNum);

        rect = new Rectangle(0,0,
            (int)page.getBBox().getWidth(),
            (int)page.getBBox().getHeight());

        image = page.getImage(rect.width , rect.height, rect, null, true, true);

        jLabel1.setIcon(new ImageIcon(image));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jLabel1 = new javax.swing.JLabel();
        nextButton = new javax.swing.JButton();
        totalPagesLabel = new javax.swing.JLabel();
        pageNumField = new javax.swing.JTextField();
        goToButton = new javax.swing.JButton();
        prevButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jScrollPane1.setViewportView(jLabel1);

        nextButton.setText(">>");
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        totalPagesLabel.setText("/ 0");

        pageNumField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        pageNumField.setText("0");

        goToButton.setText("Go to:");
        goToButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                goToButtonActionPerformed(evt);
            }
        });

        prevButton.setText("<<");
        prevButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                prevButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(prevButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(goToButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pageNumField, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(totalPagesLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(nextButton)
                .addContainerGap(571, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(nextButton)
                    .addComponent(totalPagesLabel)
                    .addComponent(pageNumField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(goToButton)
                    .addComponent(prevButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 566, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void prevButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_prevButtonActionPerformed
        if (pageNum > 1) {
            pageNum--;
            pageNumField.setText("" + pageNum);
            setPagetoImage();
        }
    }//GEN-LAST:event_prevButtonActionPerformed

    private void goToButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_goToButtonActionPerformed
        pageNum = Integer.parseInt(pageNumField.getText());
        setPagetoImage();
    }//GEN-LAST:event_goToButtonActionPerformed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        if (pageNum < totalPages) {
            pageNum++;
            pageNumField.setText("" + pageNum);
            setPagetoImage();
        }
    }//GEN-LAST:event_nextButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton goToButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton nextButton;
    private javax.swing.JTextField pageNumField;
    private javax.swing.JButton prevButton;
    private javax.swing.JLabel totalPagesLabel;
    // End of variables declaration//GEN-END:variables
}

